package com.cgy.utils.zookeeper.server;

import org.apache.curator.test.TestingServer;

import com.cgy.utils.netx.config.ServerConfig;
import com.cgy.utils.netx.server.INetworkServerLibrary;
import com.google.common.base.Preconditions;

/**
 * @author CHTK
 */
public class ZookeeperServer implements INetworkServerLibrary<TestingServer> {

    private TestingServer zkServer;

    @Override
    public INetworkServerLibrary<TestingServer> config(ServerConfig serverConfig) {
        Preconditions.checkArgument(null != serverConfig);
        Preconditions.checkArgument(serverConfig.getPort() > 0);
        try {
            zkServer = new TestingServer(serverConfig.getPort());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    @Override
    public void start() throws Exception {
        zkServer.start();
    }

    @Override
    public void stop() throws Exception {
        zkServer.close();
    }

    @Override
    public TestingServer getServer() throws Exception {
        return zkServer;
    }
}